package com.example.community.controller;

import com.example.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @Author: huang
 * @Date: 2021/10/9 9:52
 * @Description:
 */
@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    /**
     *
     * @return  访问统计页面
     */
    @RequestMapping(path = "/data", method = {RequestMethod.POST, RequestMethod.GET})
    public String DataPage() {
        return "/site/admin/data";
    }

    /**
     *
     * @param start
     * @param end
     * @return      统计网站UV
     */
    @RequestMapping(path = "/data/uv", method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long uv= dataService.calculateUv(start, end);
        model.addAttribute("uvResult", uv);
        //把日期参数放进Model里，为了让页面有显示默认的值
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "forward:/data";
    }

    /**
     *
     * @param start
     * @param end
     * @param model
     * @return      统计活跃用户
     */
    @RequestMapping(path = "/data/dau", method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long dau = dataService.calculateDAU(start, end);
        model.addAttribute("dauResult", dau);
        //把日期参数放进Model里，为了让页面有显示默认的值
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/data";
    }

}
